package com.wlgzs.booksystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlgzs.booksystem.entity.Book;
import com.wlgzs.booksystem.entity.Category;
import com.wlgzs.booksystem.entity.Collection;
import com.wlgzs.booksystem.entity.Ext.BookExt;
import com.wlgzs.booksystem.entity.Ext.Books;
import com.wlgzs.booksystem.entity.User;
import com.wlgzs.booksystem.enums.Result;
import com.wlgzs.booksystem.enums.ResultCodeEnum;
import com.wlgzs.booksystem.service.BookService;
import com.wlgzs.booksystem.service.CategoryService;
import com.wlgzs.booksystem.service.CollectionService;
import com.wlgzs.booksystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wlgzs-sjl
 * @date 2020/11/26 15:09
 */
@RestController
@RequestMapping("/index")
@Api(tags = "首页管理")
public class IndexController {
    @Autowired
    UserService userservice;
    @Autowired
    BookService bookService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    CollectionService collectionService;


    @GetMapping("/{userId}")
    @ApiOperation(value = "图书主页", httpMethod = "GET", response = Result.class)
    public Result Index(@PathVariable int userId) {
        User user = userservice.getById(userId);
        int categoryId = user.getCategoryId();

        //图书类别
        List<Category> categories = categoryService.list();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", categoryId);
        wrapper.orderByDesc("search_count");
        //搜索推荐
        List<Book> books = bookService.list(wrapper).subList(0, 4);

        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.orderByDesc("search_count");
        //搜索排行榜
        List<Book> bookList = bookService.list(wrapper1);
        List<Book> list1 = bookList.subList(0, 8);

        //主播推荐
        List<Book> list2 = bookList.subList(8, 14);

        List<Object> objects = new ArrayList<>();
        objects.add(categories);
        objects.add(books);
        objects.add(list1);
        objects.add(list2);
        return new Result(ResultCodeEnum.SUCCESS, objects);
    }

    @GetMapping("/findByName")
    @ApiOperation(value = "根据书名查找书",httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页(默认为1)", defaultValue = "1",required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每一页的大小(默认为10)", defaultValue = "10",required = true, dataType = "int"),
            @ApiImplicitParam(name = "name", value = "书名", required = true, dataType = "String")
    })
    public Result findBooksByName(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size,String name){
        Page<Book> page=new Page<>(current,size);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("book_name",name);
        List<Book> books=bookService.page(page,wrapper).getRecords();
        List<Books> bookList=new ArrayList<>();
        for (Book b:books){
            Books books1=new Books();
            books1.setCategoryName(categoryService.getById(b.getCategoryId()).getCategoryName());
            books1.setBook(b);
            bookList.add(books1);
        }
        BookExt bookExt=new BookExt(bookList,null,(page.getTotal()+size)/size);

        //图书类别
        List<Category> categories=categoryService.list();

        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.orderByDesc("search_count");
        List<Book> books1 = bookService.list(wrapper1);
        //搜索排行榜
        List<Book> list = books1.subList(0, 8);

        List<Object> objects=new ArrayList<>();
        objects.add(bookExt);
        objects.add(categories);
        objects.add(list);
        if (books!=null){
            return new Result(ResultCodeEnum.FIND,objects);
        }else {
            return new Result(ResultCodeEnum.UNFIND);
        }
    }

    @GetMapping("/findByCategory/{categoryId}")
    @ApiOperation(value = "根据类别查找书",httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页(默认为1)", defaultValue = "1",required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每一页的大小(默认为10)", defaultValue = "10",required = true, dataType = "int"),
            @ApiImplicitParam(name = "categoryId", value = "类别id", required = true, dataType = "int")
    })
    public Result findBooksByCategory(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size,@PathVariable int categoryId){
        Page<Book> page=new Page<>(current,size);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("category_id",categoryId);
        List<Book> books=bookService.page(page,wrapper).getRecords();
        List<Books> bookList=new ArrayList<>();
        for (Book b:books){
            Books books1=new Books();
            books1.setCategoryName(categoryService.getById(b.getCategoryId()).getCategoryName());
            books1.setBook(b);
            bookList.add(books1);
        }
        BookExt bookExt=new BookExt(bookList,null,(page.getTotal()+size)/size);

        //图书类别
        List<Category> categories=categoryService.list();

        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.orderByDesc("search_count");
        List<Book> books1 = bookService.list(wrapper1);
        //搜索排行榜
        List<Book> list = books1.subList(0, 8);

        List<Object> objects=new ArrayList<>();
        objects.add(bookExt);
        objects.add(categories);
        objects.add(list);
        if (books!=null){
            return new Result(ResultCodeEnum.FIND,objects);
        }else {
            return new Result(ResultCodeEnum.UNFIND);
        }
    }

    @GetMapping("/book/{bookId}")
    @ApiOperation(value = "图书详情页", httpMethod = "GET", response = Result.class)
    public Result bookIndex(@PathVariable int bookId) {
        Book book = bookService.getById(bookId);
        return new Result(ResultCodeEnum.SUCCESS, book);
    }

    @GetMapping("/collection/{userId}")
    @ApiOperation(value = "收藏详情页", httpMethod = "GET", response = Result.class)
    public Result collectionIndex(@PathVariable int userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        List<Collection> collections = collectionService.list(wrapper);
        List<Book> books = new ArrayList<>();
        if (collections != null) {
            for (Collection collection : collections) {
                books.add(bookService.getById(collection.getBookId()));
            }
        }
        return new Result(ResultCodeEnum.SUCCESS, books);
    }
}
